﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YJKRevitDimensionText.Utility;

namespace YJKRevitDimensionText.DimCreator
{
	internal class ConnectionDimensionOperation : HYCmdEntry
	{
		public ConnectionDimensionOperation(ExternalCommandData commandData) : base(commandData)
		{
		}

		public void Excute()
		{
			try
			{
				RevitVersionFuncs.ClearSelection(this.m_sel);
				ISelectionFilter selectionFilter = new ConnectionDimensionFilter();
				ConnectionDimension connectionDimension = new ConnectionDimension(this.m_doc);
				Reference reference = this.m_sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择要连接的尺寸标注");
				Dimension dimension = this.m_doc.GetElement(reference) as Dimension;
				ISelectionFilter selectionFilter2 = new TargetConnectionDimensionFilter(dimension);
				List<Dimension> targetDims = (from e in this.m_sel.PickElementsByRectangle(selectionFilter2, "框选要连接的尺寸标注")
				select e as Dimension).ToList<Dimension>();
				for (;;)
				{
					dimension = connectionDimension.Connection(dimension, targetDims);
					if (dimension == null)
					{
						break;
					}
					selectionFilter2 = new TargetConnectionDimensionFilter(dimension);
					targetDims = (from e in this.m_sel.PickElementsByRectangle(selectionFilter2, "框选要连接的尺寸标注")
					select e as Dimension).ToList<Dimension>();
				}
				throw new Exception();
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
			}
			catch (Exception)
			{
				YJKMessageBox.Show("连接错误");
			}
		}
	}
}
